package com.blog.zip.service;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Calendar;

@Slf4j
@Service

public class GitalkService {

    // 获取百度统计access_token的相关文档，https://blog.csdn.net/qq_23138349/article/details/121712018
    /**
     * 百度账号
     * API Key：y5GMcdliT92AAUPY0357pVwRrkhtUxGg
     * Secret Key：EEsoa4Q8xrpoIk20olrbKOF11qUpPDn4
     * 链接：http://openapi.baidu.com/oauth/2.0/authorize?response_type=code&client_id=y5GMcdliT92AAUPY0357pVwRrkhtUxGg&redirect_uri=oob&scope=basic&display=popup
     * 授权码：05e395f341fbe94aba29c73c58d0f9fb
     * http://openapi.baidu.com/oauth/2.0/token?grant_type=authorization_code&code=05e395f341fbe94aba29c73c58d0f9fb&client_id=y5GMcdliT92AAUPY0357pVwRrkhtUxGg&client_secret=EEsoa4Q8xrpoIk20olrbKOF11qUpPDn4&redirect_uri=oob
     * access_token：121.42ef1b62cc5036fff7ede9f6ee85b7d9.Y_HQganrkooS9zBAk8rzVDk42WKEQb4AvcxJxVT.OASfzA
     * refresh_token：122.06f7819db020252e9211d4d15e462a4d.YHtoK_uQigT6ZjzvtqO0icsb_H8dLS4pooG5J38.b2JeCw
     * https://openapi.baidu.com/rest/2.0/tongji/report/getData?access_token=121.42ef1b62cc5036fff7ede9f6ee85b7d9.Y_HQganrkooS9zBAk8rzVDk42WKEQb4AvcxJxVT.OASfzA&site_id=17994255&method=overview/getTimeTrendRpt&start_date=20220520&end_date=20230105&metrics=pv_count,visitor_count,ip_count
     * param response
     * 更新access_token相关文档
     * http://openapi.baidu.com/oauth/2.0/token?grant_type=refresh_token&refresh_token={REFRESH_TOKEN}&client_id={CLIENT_ID}&client_secret={CLIENT_SECRET}
     * https://tongji.baidu.com/api/manual/Chapter2/openapi.html
     * @return
     */
    public static JSONObject getDataByCityName() {
        //String uri = "https://api.github.com/repos/Arrowfield/solo-blog/issues";
        Calendar c = Calendar.getInstance();
        String year = c.get(Calendar.YEAR) + "";
        int month = c.get(Calendar.MONTH) + 1;
        int date = c.get(Calendar.DATE) ;
//        month = month < 10 ? '0' + month : month;
//        date = date < 10 ? '0' + date : date;
        String access_token = "121.42ef1b62cc5036fff7ede9f6ee85b7d9.Y_HQganrkooS9zBAk8rzVDk42WKEQb4AvcxJxVT.OASfzA";
        String start_date = "20220520";
        // 0（数字零）的ASCII代码为048
        //log.info("拼接后的月份{}",'0' + month);
        String end_date = year  + (month < 10 ? '0' + String.valueOf(month) : month + "")  + (date < 10 ? '0' + String.valueOf(date) : date +"");
        String method = "source/all/a";

        String uri = "https://openapi.baidu.com/rest/2.0/tongji/report/getData?" +
            "access_token=" + access_token +
            "&site_id=17994255" +
            "&method=" + method +
            "&start_date=" + start_date +
            "&end_date=" + end_date +
            "&metrics=pv_count,ip_count";



            //?access_token=xxxxxxxx&site_id=xxxx&method=overview/getTimeTrendRpt&start_date=20190101&end_date=20190105&metrics=pv_count,visitor_count,ip_count
//        JSONObject object=new JSONObject();
//        object.put("access_token",access_token);
//        object.put("start_date",start_date);
//        object.put("end_date",end_date);
//        object.put("method","overview/getTimeTrendRpt");
//        object.put("metrics","pv_count,visitor_count,ip_count");
//        object.put("site_id","17994255");
//        String jsonData = object.toString();

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<>(headers);
        String strBody = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class).getBody();
//        log.info("{}",strBody);
        JSONObject weatherResponse = JSONObject.parseObject(strBody);
//log.info("{}", weatherResponse);
        return weatherResponse;
    }
}
